﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TCHSHue.BLL;

namespace TCHSHue.Modules.IndexControl.TimKiem
{
    public partial class ctrTimKiem : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int pCurrent = getPageCurrent();
                var key = Request.QueryString["key"];
                if (!string.IsNullOrEmpty(key))
                    NapDanhSachTimKiem(Server.HtmlDecode(key), pCurrent);
            }
        }

        void NapDanhSachTimKiem(string key, int pCurrent)
        {
            var ls = BaiVietBLL.getListTheoTuKhoa(key, Hang.PAGESIZE, pCurrent);
            ddlTinTucSuKien.DataSource = ls;
            ddlTinTucSuKien.DataBind();
            var tsBai = BaiVietBLL.getListTheoTuKhoaToTal(key);
            PhanTrang(tsBai, pCurrent, "../Modules/Index.aspx?mode=timkiem&key=" + Server.HtmlEncode(key) + "&");
        }

        int getPageCurrent()
        {
            var pageIndex = Request.QueryString["pageIndex"];
            int temp;
            int pCurrent = 1;
            if (!int.TryParse(pageIndex, out temp))
                pCurrent = 1;
            else
                pCurrent = int.Parse(pageIndex);
            return pCurrent;
        }

        void PhanTrang(int total, int pCurrent, string baseLink)
        {
            Paging paGing = new Paging(Hang.PAGESIZE);
            string resultPaging = paGing.CreatePaging(total, pCurrent, baseLink);
            if (string.IsNullOrEmpty(resultPaging))
                divPaging.Visible = false;
            else
            {
                divPaging.InnerHtml = resultPaging;
            }
        }
    }
}